package com.gy.api.pc.clap.controller;

//import cn.afterturn.easypoi.excel.ExcelExportUtil;
//import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.ApiIndex;
import com.gy.api.base.R;
import com.gy.api.base.RestPage;
import com.gy.api.pc.clap.domain.vo.*;
import com.gy.api.pc.clap.service.IAssociationClapCulvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.net.URLEncoder;


@RestController
@RequestMapping("/association/clap/culvert")
@RequiredArgsConstructor
@Api( tags = ApiIndex.ClapCulvertController)
public class ClapCulvertController {

    private final IAssociationClapCulvertService associationClapCulvertService;



    @GetMapping("/page")
    @ApiOperation(value = "协拍涵管理-列表 ")
    public R<RestPage<List<PageCulvertVo>>> pageCulvert(@ModelAttribute PageCulvertDto dto) throws Exception {
        List<PageCulvertVo> list = associationClapCulvertService.pageCulvert(dto);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "协拍涵管理-详情（根据流程ID查询）")
    public R<CulvertDetailVO> getApprovalDetail(@RequestParam String id) {
        CulvertDetailVO culvertVo = associationClapCulvertService.getApprovalDetail(id);
        return new R(culvertVo);
    }

    @PostMapping("/harmonize")
    @ApiOperation(value = "协拍涵管理-反馈协调情况-协调接口/单个协调/全部协调")
    public R<String> harmonizeCulvert(@Validated @RequestBody CulvertHarmonizeDto dto) throws Exception {
        List<String> ids = dto.getIds();
        associationClapCulvertService.harmonizeCulvert(ids);
        return new R();
    }

//    @PostMapping("/export")
//    @ApiOperation(value = "协拍涵清单导出-未完成")
//    public void export(@Validated @RequestBody CulvertHarmonizeDto dto, HttpServletResponse response) throws IOException {
//        List<CulvertApprovalDetailExportVo> export = associationClapCulvertService.export(dto);
//        // 导出参数设置
//        ExportParams exportParams = new ExportParams("重庆市影视拍摄协调服务函", "协拍涵");
//
//        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CulvertApprovalDetailExportVo.class, export);
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("UTF-8");
//        String fileName = URLEncoder.encode("culvert_approval_detail.xlsx", "UTF-8");
//        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
//        try (ServletOutputStream out = response.getOutputStream()) {
//            workbook.write(out);
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (workbook != null) {
//                try {
//                    ((AutoCloseable) workbook).close();
//                } catch (Exception ex) {
//                    // 忽略关闭异常
//                }
//            }
//
//        }
//
//    }
}